<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('contract.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-form-item :label="i18n('contract.chinese_title')" prop="chinese_title">
                <el-input v-model="dataForm.chinese_title" :placeholder="i18n('common.please_input') + i18n('contract.chinese_title')"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('contract.english_title')" prop="english_title">
                <el-input v-model="dataForm.english_title" :placeholder="i18n('common.please_input') + i18n('contract.english_title')"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('contract.original_money')" prop="original_money">
                <el-input-number v-model="dataForm.original_money" :min="0" :placeholder="i18n('common.please_input') + i18n('contract.original_money')">
                </el-input-number>
              </el-form-item>

              <el-form-item :label="i18n('contract.money')" prop="money">
                <el-input-number v-model="dataForm.money" :min="0" :placeholder="i18n('common.please_input') + i18n('contract.money')">
                </el-input-number>
              </el-form-item>

              <el-form-item :label="i18n('contract.pay_money')" prop="pay_money">
                <el-input-number v-model="dataForm.pay_money" :min="0" :placeholder="i18n('common.please_input') + i18n('contract.pay_money')">
                </el-input-number>
              </el-form-item>

              <el-form-item :label="i18n('contract.rate')" prop="rate">
                <el-input-number v-model="dataForm.rate" :min="0" :max="100" :placeholder="i18n('common.please_input') + i18n('contract.rate')">
                </el-input-number>
              </el-form-item>

              <el-form-item :label="i18n('contract.period')" prop="period">
                <el-input-number v-model="dataForm.period" :min="0" :max="12" :placeholder="i18n('common.please_input') + i18n('contract.period')">
                </el-input-number>
              </el-form-item>

              <el-form-item :label="i18n('contract.value')" prop="value">
                <el-input-number v-model="dataForm.value" :min="0" :max="31" :placeholder="i18n('common.please_input') + i18n('contract.value')">
                </el-input-number>
              </el-form-item>

              <el-form-item :label="i18n('contract.description')" prop="description">
                <el-input
                  type="textarea"
                  v-model="dataForm.description"
                  :rows="3"
                  show-word-limit
                  maxlength="200"
                  :placeholder="i18n('common.please_input') + i18n('contract.description')">
                </el-input>
              </el-form-item>

              <el-form-item :label="i18n('common.sort')" prop="sort">
                <el-input-number :placeholder="i18n('common.please_input') + i18n('common.sort')" v-model="dataForm.sort"></el-input-number>
              </el-form-item>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.detail_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-form-item class="mavon" prop="content" :label="i18n('contract.content')">
                <editor ref="editorRef" :content="dataForm.content"></editor>
              </el-form-item>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:contract:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="contract-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import Editor from '@/components/form/ckeditor'

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('contract')

  const editorRef = ref()

  const dataForm = reactive({
    id: 0,
    category_id: '',
    chinese_title: '',
    english_title: '',
    description: '',
    content: '',
    money: 0,
    pay_money: 0,
    original_money: 0,
    period: 0,
    value: 0,
    rate: 0,
    sort: 0,
  })

  const dataRule = reactive({
    chinese_title: [{
      required: true,
      message: i18n('contract.rules.chinese_title.require'),
      trigger: 'blur',
    }],
    english_title: [{
      required: true,
      message: i18n('contract.rules.english_title.require'),
      trigger: 'blur',
    }],
    money: [{
      required: true,
      message: i18n('contract.rules.money.require'),
      trigger: 'blur',
    }],
    pay_money: [{
      required: true,
      message: i18n('contract.rules.pay_money.require'),
      trigger: 'blur',
    }],
    original_money: [{
      required: true,
      message: i18n('contract.rules.original_money.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.category_id = data.data.category_id
            dataForm.chinese_title = data.data.chinese_title
            dataForm.english_title = data.data.english_title
            dataForm.description = data.data.description
            dataForm.money = data.data.money
            dataForm.pay_money = data.data.pay_money
            dataForm.original_money = data.data.original_money
            dataForm.period = data.data.period
            dataForm.value = data.data.value
            dataForm.rate = data.data.rate
            dataForm.sort = data.data.sort
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/detail/data`),
              method: 'get',
              params: $http.adornParams({
                'contract_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data.data && data.status === 200) {
                dataForm.content = data.data.content
              }
            })
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            category_id: dataForm.category_id,
            chinese_title: dataForm.chinese_title,
            english_title: dataForm.english_title,
            description: dataForm.description,
            content: editorRef.value.data,
            money: dataForm.money,
            pay_money: dataForm.pay_money,
            original_money: dataForm.original_money,
            period: dataForm.period,
            value: dataForm.value,
            rate: dataForm.rate,
            sort: dataForm.sort,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  if(query.category_id) {
    dataForm.category_id = parseInt(query.category_id)
  }

  onMounted(() => {
    init(formRef)
  })
</script>
